package utils;

import java.util.ArrayList;
import java.util.Collections;

public class Vector{
	private ArrayList<Double> vector;
	public Vector(ArrayList<Double> c){
		this.vector=c;
	}
	
	public ArrayList<Double> getArrayList(){
		return this.vector;
	}
	public String toString(){
		return this.vector.toString();
	}
	public double multiply(Vector vec){
		double result=0.0;
		assert this.size()==vec.size();
		
		for(int i=0; i<this.size(); i++){
			result+=this.get(i)*vec.get(i);
		}
		return result;
		
	}
	
	public void normalise(){
		double length=0.0;
		//ArrayList<Double> normalizedList=new ArrayList<Double>();
		for(double  f : vector){
			length+=Math.pow(f , 2);
		}
		length=Math.sqrt(length);
		for(int i=0; i<vector.size();i++){
			vector.set(i , vector.get(i)/length);
		}
	}
	
	public double calculateDistance(Vector v){
		if(v.getArrayList().size()!=this.vector.size())
			return -1;
		double distance=0;
		for(int i=0; i <this.vector.size(); i++){
			distance+=Math.pow(this.vector.get(i)-v.getArrayList().get(i) , 2);	
		}
		distance=Math.sqrt(distance);
		return distance;
	}
	public int size(){
		return this.vector.size();
	}
	
	public Vector add(Vector addop){
		if(addop==null)
				return this;
		if(addop.size()!=this.vector.size())
				return null;
		ArrayList<Double> result=new ArrayList<Double>(this.size());
		for(int i=0 ; i< addop.size(); i++){
			//result.set(i, addop.get(i)+this.vector.get(i));
			result.add(addop.get(i)+this.vector.get(i));
		}
		return new Vector(result);
	}
	
	public Vector sort(){
		ArrayList<Double> newVec=new ArrayList<Double>(this.vector);
		Collections.sort(newVec);
		return new Vector(newVec);
	}
	
	public Double[] getTop(int N){
		Double[] topN=new Double[N];
		Vector sorted=this.sort();
		for(int i=0; i<N; i++){
			topN[i]=sorted.get(sorted.size()-i-1);
		}
		return topN;
	}
	
	public double get(int i){
		return this.vector.get(i);
	}
	public boolean equals(Object obj){
		if(this==obj)
			return true;
		if(!(obj instanceof Vector))
			return false;
		Vector v=(Vector)obj;
		if (v.size()!=this.size())
				return false;
		for(int i=0; i<this.size(); i++){
			if(Math.abs(this.get(i)-v.get(i))>0.05){
				return false;
			}
		}
		return true;
	}
}
